from .data import data_get,get_train,data_tolist

from sklearn.ensemble import GradientBoostingClassifier,AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier

x,y = data_get()

model_AdaBoostClassifier = AdaBoostClassifier(
        DecisionTreeClassifier(max_depth=1,
        min_samples_split=2,
        min_samples_leaf=1),
        learning_rate=0.1,
        n_estimators=150)

model_GradientBoostingClassifier = GradientBoostingClassifier(
    max_depth=1,                                   
    min_samples_leaf=2, 
    n_estimators=10)

def get_model_param():
    return ['max_depth','min_samples_split','min_samples_leaf','learning_rate','n_estimators','random_state']
#导入数据集
def data_loan():
    """
     data loan
    """
    columns = x.columns.tolist()
    for i in range(len(columns)):
        columns[i] = columns[i].replace(' ','_')
    data = x.values.tolist()
    return x, columns, data 

def static_columns():
    org = get_train()
    static_col = []
    for k, v in org.iteritems():
        item = v.value_counts()
        if len(item)<=8: static_col.append([k,item.index.tolist(),item.values.tolist()]) 
    return static_col

def static_corr():
    org = get_train()
    org = org.drop('Loan_Status',axis=1)
    org_corr = data_tolist(org.corr())
    now_corr = data_tolist(x.corr())
    org_corr = data_corr(org_corr)
    now_corr = data_corr(now_corr)
    return org_corr,now_corr

def data_corr(corr_arr):
    l=[]
    for i in range(len(corr_arr[1])):
        for j in range(len(corr_arr[1])):
            l.append([i,j,round(corr_arr[1][i][j],3)])
    corr_arr[1] = l
    return corr_arr


def train_AdaBoostClassifier(data):
    global model_AdaBoostClassifier
    
    model_param=get_model_param()

    param={}
    if data:
        for i in range(len(model_param)):
            if data[i]:
                param[model_param[i]]=data[i]
    
    model_AdaBoostClassifier.fit(x, y)


def predict_AdaBoostClassifier(data):
    global model_AdaBoostClassifier
    return model_AdaBoostClassifier.predict([data])[0]


def train_GradientBoostingClassifier(data):
    global model_GradientBoostingClassifier
    model_param=get_model_param()

    param={}
    if data:
        for i in range(len(model_param)):
            if data[i]:
                param[model_param[i]]=data[i]
                
    model_GradientBoostingClassifier.fit(x, y)

def predict_GradientBoostingClassifier(data):
    global model_GradientBoostingClassifier
    return model_GradientBoostingClassifier.predict([data])[0]

